import { Component, OnInit } from '@angular/core';
import { NavParams } from 'ionic-angular';
import { DetailService } from './detail.service';

@Component({
  templateUrl: 'detail.component.html',
  providers: [ DetailService ]
})
export class DetailPage implements OnInit {
  task:any;
  apply:any;
  process:Array<any> = [5];
  materialList:Array<any> = [];
  
  constructor(
    private navParams: NavParams,
    private detailService: DetailService) {
    this.task = this.navParams.get('task');
    this.apply = this.navParams.get('apply');
  }

  ngOnInit() {
      this.detailService.getMaterialApprovalLog(this.task.taskID).subscribe(data => {
         var logs = data['approvalLog'];
         var flag = 0;
         logs.forEach(e => {
           if(e.applyID == null && e.approvalLevel == 1){
              this.apply['firstApprovalDate'] = e.createTime;
            }
           if(e.applyID == null && e.approvalLevel == 2){
              if(flag < 1) flag = 1;
              this.apply['secondApprovalDate'] = e.createTime;
            }
            if(e.applyID == null && e.approvalState == 4){
              if(flag < 2) flag = 2;
            }
           if(e.applyID == null && e.approvalState == 5){
              if(flag < 3) flag = 3;
              this.apply['purchaseDate'] = e.createTime;
            }
           if(e.applyID != null && e.approvalState == 6){
              if(flag < 4) flag = 4;
              this.apply['recieveDate'] = e.createTime;
            }
         });
         this.process[flag] = "in-process";
      });

      this.detailService.getapplymateriallist(this.apply.applyID).subscribe(data => {
        this.materialList = data['materialList'];
      });
  }
}
